/* This file aims to produce summary statistics and run regressions (including reduced-form analysis and estimating policy functions   */
clear all
set more off


***%%%*** Vendor characteristics accounting for BED size
clear
cd ""   //target directory


insheet using exov1mktbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mktbed`i'
}
reshape long newv1mktbed,i(ahaid year) j(vid)
tempfile v1mktbed
save `v1mktbed'



insheet using exov2mktbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mktbed`i'
}
reshape long newv2mktbed,i(ahaid year) j(vid)
tempfile v2mktbed
save `v2mktbed'


insheet using exovmkshbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktshbed`i'
}
reshape long newvmktshbed,i(ahaid year) j(vid)
tempfile vmktshbed
save `vmktshbed'


insheet using v1sysbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sysbed`i'
}
reshape long newv1sysbed,i(ahaid year) j(vid)
tempfile v1sysbed
save `v1sysbed'


insheet using v2sysbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sysbed`i'
}
reshape long newv2sysbed,i(ahaid year) j(vid)
tempfile v2sysbed
save `v2sysbed'


insheet using vsyshbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsyshbed`i'
}
reshape long newvsyshbed,i(ahaid year) j(vid)
tempfile vsyshbed
save `vsyshbed'


*** IVs


clear
insheet using "./prepare/iv_dombed.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdombed`i'
}
reshape long ivdombed,i(hsayr_id) j(vid)
tempfile ivdombed
save `ivdombed'


insheet using "./prepare/iv_mshbed.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmshbed`i'
}
reshape long ivmshbed,i(hsayr_id) j(vid)
tempfile ivmshbed
save `ivmshbed'





***@@@*** Vendor characteristics accounting for PATIENT size
clear
cd ""    //target directory


insheet using exov1mktbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mktpat`i'
}
reshape long newv1mktpat,i(ahaid year) j(vid)
tempfile v1mktpat
save `v1mktpat'



insheet using exov2mktbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mktpat`i'
}
reshape long newv2mktpat,i(ahaid year) j(vid)
tempfile v2mktpat
save `v2mktpat'


insheet using exovmkshbed.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktshpat`i'
}
reshape long newvmktshpat,i(ahaid year) j(vid)
tempfile vmktshpat
save `vmktshpat'


*** IVs


clear
insheet using "./prepare/iv_dombed.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdompat`i'
}
reshape long ivdompat,i(hsayr_id) j(vid)
tempfile ivdompat
save `ivdompat'


insheet using "./prepare/iv_mshbed.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmshpat`i'
}
reshape long ivmshpat,i(hsayr_id) j(vid)
tempfile ivmshpat
save `ivmshpat'





***+++*** General vendor characteristics
clear
cd ""    //target directory


insheet using choice.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newchoice`i'
}
reshape long newchoice,i(ahaid year) j(vid)
tempfile chs
save `chs'

insheet using vlag.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvlag`i'
}
reshape long newvlag,i(ahaid year) j(vid)
tempfile vlag
save `vlag'


insheet using exov1mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1mkt`i'
}
reshape long newv1mkt,i(ahaid year) j(vid)
tempfile v1mkt
save `v1mkt'



insheet using exov2mkt.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2mkt`i'
}
reshape long newv2mkt,i(ahaid year) j(vid)
tempfile v2mkt
save `v2mkt'


insheet using exovmksh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvmktsh`i'
}
reshape long newvmktsh,i(ahaid year) j(vid)
tempfile vmktsh
save `vmktsh'


insheet using v1sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv1sys`i'
}
reshape long newv1sys,i(ahaid year) j(vid)
tempfile v1sys
save `v1sys'


insheet using v2sys.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newv2sys`i'
}
reshape long newv2sys,i(ahaid year) j(vid)
tempfile v2sys
save `v2sys'


insheet using vsysh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvsysh`i'
}
reshape long newvsysh,i(ahaid year) j(vid)
tempfile vsysh
save `vsysh'



//IV inputs  ### UPDATED on 07/24/2019: Philipp mentioned using an alternative IV, which is the vendor share within the multi-region chain; input for IV is altiv_dom/msh_tmp.csv
clear
insheet using "./prepare/iv_dom_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivdom`i'
}
reshape long ivdom,i(hsayr_id) j(vid)
tempfile ivdom
save `ivdom'


insheet using "./prepare/iv_msh_tmp.csv",clear
rename v14 hsayr_id
forvalues i=1/13 {
rename v`i' ivmsh`i'
}
reshape long ivmsh,i(hsayr_id) j(vid)
tempfile ivmsh
save `ivmsh'



insheet using exovtipmsh.csv,clear
rename v14 ahaid
rename v15 year
forvalues i=1/13 {
rename v`i' newvtipmsh`i'
}
reshape long newvtipmsh,i(ahaid year) j(vid)
merge 1:1 ahaid year vid using `chs'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vlag'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mkt'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktsh'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v1sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2sys'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vsysh'
keep if _merge==3
drop _merge


merge 1:1 ahaid year vid using `v1mktbed'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mktbed'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktshbed'
keep if _merge==3
drop _merge



merge 1:1 ahaid year vid using `v1mktpat'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `v2mktpat'
keep if _merge==3
drop _merge
merge 1:1 ahaid year vid using `vmktshpat'
keep if _merge==3
drop _merge



tempfile vtipmsh
save `vtipmsh'




**********
**********
********** Combine all
clear
use "./prepare/allbasic.dta",clear
merge 1:m ahaid year using `vtipmsh'
keep if _merge==3
drop _merge


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr_id=group(hsa year)



********** Merge with IVs
merge m:1 hsayr_id vid using `ivmsh'
drop _merge

merge m:1 hsayr_id vid using `ivdom'
drop _merge


merge m:1 hsayr_id vid using `ivdombed'
drop _merge

merge m:1 hsayr_id vid using `ivmshbed'
drop _merge



merge m:1 hsayr_id vid using `ivdompat'
drop _merge

merge m:1 hsayr_id vid using `ivmshpat'
drop _merge




********** Merge with market characterisitcs (demographics and market-category effects)
******  Demographics: population and income per capita
preserve
clear
use "./prepare/demographic4mkt"
duplicates drop hsa year,force
keep hsa year totpop totpop65 avginc
rename totpop65 totpop65_ori
gen perc65=totpop65_ori/totpop
replace totpop=log(totpop)
gen totpop65=log(totpop65_ori)
replace avginc=log(avginc)
tempfile pop
save `pop'
restore
******


****** Create HHI at the market level using total admissions: admtot
preserve
keep if vid==13
keep id year admtot hsa
bys hsa year:egen totadm_mkt=total(admtot)
gen admsh=admtot/totadm_mkt
bys hsa year:egen hhi=total(admsh^2)
duplicates drop hsa year,force
keep hsa year hhi
tempfile hhi
save `hhi'
restore
******


****** Market-category effects (using adoption ratio as DV)
preserve

clear
input hsa mktfe
1 0
end
tempfile firsthsa
save `firsthsa'


clear
import delim using "./prepare/mktFEs_comb1.csv",delimiters(",") varnames(1)
rename (v1 comb) (hsa mktfe)
drop if strpos(hsa,"_Ihsa_")==0
replace hsa=subinstr(hsa,"_Ihsa_","",1)
destring,replace force
append using `firsthsa',force

gen below25cat=0
gen below50cat=0
gen below75cat=0
gen below100cat=0

sum mktfe,detail

replace below25cat=1 if mktfe<=r(p25)
replace below50cat=1 if mktfe<=r(p50)&mktfe>r(p25)
replace below75cat=1 if mktfe<=r(p75)&mktfe>r(p50)
replace below100cat=1 if mktfe>r(p75)

tempfile mktfe_comb
save `mktfe_comb'

restore
******


****** Merge files

merge m:1 hsa using `mktfe_comb'
keep if _merge==3
drop _merge


merge m:1 hsa year using `pop'
keep if _merge==3
drop _merge


merge m:1 hsa year using `hhi'
keep if _merge==3
drop _merge





********** Create bed size quartiles
gen bdrng=0
sum bdtot if sysid==0,detail  

replace bdrng=1 if bdtot<=r(p25)
replace bdrng=2 if bdtot>r(p25) & bdtot<=r(p50)
replace bdrng=3 if bdtot>r(p50) & bdtot<=r(p75)
replace bdrng=4 if bdtot>r(p75)
tab bdrng,gen(bdqntl)


foreach i in 1 2 3 4 {
gen v1bd`i'=newv1mkt*bdqntl`i'
gen vmshbd`i'=newvmktsh*bdqntl`i'
gen vlagbd`i'=newvlag*bdqntl`i'
}


forvalues i=2/13 {
gen bed`i'=bdtot*(vid==`i')
replace bed`i'=bed`i'/100
}


gen adopt=(adj_vnd>1)
bys hsa year vid:gen nhosp=_N



********** Prepare files for structural estimation
****** "regvar`i'_sing"
forvalues i=6/9 {
preserve
keep if year==2000+`i'
keep if newchoice==1
keep if sysid==0
sort ahaid
outfile ahaid year bdtot prechs adj_vnd hsa using "./prepare/regvar`i'_sing",c replace
restore
 }


****** "mktvar_pophhi"
gen mktcat=0
replace  mktcat=0 if below25cat==1
replace  mktcat=1 if below50cat==1
replace  mktcat=2 if below75cat==1
replace  mktcat=3 if below100cat==1
keep if newchoice==1

sort ahaid year
outfile ahaid year hhi mktcat totpop65 using "./prepare/mktvar_pophhi" if sysid==0,c replace
drop mktcat



********** Prepare inputs for bootstrapping (Main specification)
****** generate the set of HSAs for bootstrapping; samples generated in Matlab
preserve
keep if newchoice==1
duplicates drop hsa year,force
keep if year==2006
keep hsa nhosp
sort hsa
export delim hsa nhosp using "./prepare/list_mkt.csv",delim(",") novar replace 
restore



****** Inputs: MUST include HSA, because bootstrapping is blocked by HSA; use "ahaid"!!!
** Create matrix Y --- N x 1 vector of integers 1 through J indicating which alternative was chosen
preserve
keep if newchoice==1
sort ahaid year
export delim ahaid year hsa adj_vnd prechs sysid using "./prepare/matY.csv",delim(",") novar replace 
restore


** Create matrix X --- N x K1 matrix of individual-specific covariates
preserve
keep if newchoice==1
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

sort ahaid year
export delim ahaid year hsa bdtot prechs nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/matX.csv",delim(",") novar replace 
restore


** Create matrix Z --- N x K2 x J array of covariates that are alternative-specific
preserve
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

sort ahaid year vid
export delim ahaid year vid hsa vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd1 vlagbd2 vlagbd3 vlagbd4 newvlag newvsysh below50cat below75cat below100cat prechs sysid using "./prepare/matZ.csv",delim(",") novar replace 
restore







********** Prepare inputs for bootstrapping (decreasing sunk costs)
****** Create matrix Y --- N x 1 vector of integers 1 through J indicating which alternative was chosen
preserve
keep if newchoice==1
sort ahaid year
export delim ahaid year hsa adj_vnd prechs sysid using "./prepare/matY.csv",delim(",") novar replace 
restore


****** Create matrix X --- N x K1 matrix of individual-specific covariates //### UPDATED on 12/03/2020: include yrsince
preserve
keep if newchoice==1
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

sort ahaid year
export delim ahaid year hsa bdtot prechs nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 yrsince sysid using "./prepare/matX.csv",delim(",") novar replace 
restore


****** Create matrix Z --- N x K2 x J array of covariates that are alternative-specific //### UPDATED on 12/03/2020: include newv1mkt for yrsinceXmktDOM
preserve
gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

sort ahaid year vid
export delim ahaid year vid hsa vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd1 vlagbd2 vlagbd3 vlagbd4 newvlag newvsysh below50cat below75cat below100cat vdom_yrsince prechs sysid using "./prepare/matZ.csv",delim(",") novar replace 
restore







********** ### Summary statistics (Table 1) ###
preserve
//find out % switching
gen swch=(prechs~=adj_vnd)*(prechs>1)
//find out % using mktdom and % adopt at the market level
gen findv1mkt=newv1mkt*vid
gen isv1mkt=(prechs==findv1mkt)
bys ahaid year:egen maxusedom=max(isv1mkt)
bys hsa year vid:egen totusedom=total(maxusedom)
bys hsa year vid:egen totadp=total(adopt)
gen perc_adp=totadp/nhosp
gen perc_usedom=totusedom/totadp
replace perc_usedom=0 if totadp==0
replace perc_usedom=1 if nhosp==1&totadp>0

//find out # standalone hospitals per HSA
gen beSA=(sysid==0)
bys hsa year vid:egen nsa=total(beSA)


keep if newchoice==1
//adoption and switching rates
bys year:sum adopt swch if sysid==0
//hospital characteristics
bys year:sum bdtot nprofit profit teach perc_mcr perc_mcd if sysid==0
//market characteristics
duplicates drop hsa year,force
bys year:sum nhosp nsa perc_adp totpop65_ori hhi
bys year:sum perc_usedom if totadp>0

restore




********** ### Reduced-form analysis (Table 2; Table A2-A4) ###

****** CF 1st step
reg newvmktsh ivdom ivmsh if prechs==1&sysid==0
predict uhatori,residual

gen uhatori2=uhatori*uhatori
gen uhatori3=uhatori*uhatori*uhatori


****** Table 2, A2-A4
xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(newid) vce(r)  
est sto noniv

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0&uhatori~=.,group(newid) vce(r)  
est sto noniv_iv

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* uhatori uhatori2 i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(newid) vce(boot,rep(100))  
est sto iv




********** ### Policy functions (Table A8 -- A11) ###
****** Standalone, NA
forvalues i=6/9 {
preserve
set more off
keep if sysid==0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 i.vid*bdtot i.vid*nprofit i.vid*teaching below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_na`i'.csv",delim(",") novar replace 

restore
esttab ab using "./results/coef_sana.csv", replace not nogaps compress nostar
}


****** Standalone, AD
forvalues i=6/9 {
preserve
set more off
keep if sysid==0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd* i.vid*bdtot i.vid*profit i.vid*teaching i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_a`i'.csv",delim(",") novar replace

restore
esttab ab using "/results/coef_saad.csv", replace not nogaps compress nostar
}


****** Affiliated, NA
forvalues i=6/9 {
preserve
set more off
keep if sysid>0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit newchoice newvsysh i.vid*bdtot i.vid*nprofit i.vid*perc_mcr below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_na`i'.csv",delim(",") novar replace

restore
esttab ab using "./results/coef_afna.csv", replace not nogaps compress nostar
}


****** Affiliated, AD
forvalues i=6/9 {
preserve
set more off
keep if sysid>0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

xi:clogit newchoice newvsysh vlag i.vid*bdtot i.vid*perc_mcr i.vid*perc_mcd i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_a`i'.csv",delim(",") novar replace
restore
esttab ab using "./results/coef_afad.csv", replace not nogaps compress nostar
}







********** ### Robustness check: LPM and 2SLS (Table A5-A6) ###
preserve
keep if sysid==0
keep if prechs==1


foreach i in 1 2 3 4 {
gen iv1bd`i'=ivdom*bdqntl`i'
gen ivmshbd`i'=ivmsh*bdqntl`i'
}



xi:ivreg2 newchoice i.vid*bdtot (vmshbd1 vmshbd2 vmshbd3 vmshbd4=iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4) below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,robust ffirst // F-stat: 10.5
est sto iv
xi:ivreg2 newchoice i.vid*bdtot vmshbd1 vmshbd2 vmshbd3 vmshbd4 below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if ivmsh~=.,robust 
est sto noiv2
xi:ivreg2 newchoice i.vid*bdtot vmshbd1 vmshbd2 vmshbd3 vmshbd4 below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,robust 
est sto noiv1
esttab noiv1 noiv2 iv using "./results/static_model_2SLS.tex", replace se not keep(vmshbd1 vmshbd2 vmshbd3 vmshbd4) scalars(N r2) nogaps star(* 0.10 ** 0.05 *** 0.01)


** export 1st-stage results
foreach i of num 1/4 {
xi:reg vmshbd`i' iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4 i.vid*bdtot below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,robust
est sto iv1st`i'
test iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4
estadd scalar fstat=r(F)
estadd scalar pv=r(p)
}
esttab iv1st1 iv1st2 iv1st3 iv1st4 using "./results/first_stage_reported.tex", replace se not keep(iv1bd1 iv1bd2 iv1bd3 iv1bd4 ivmshbd1 ivmshbd2 ivmshbd3 ivmshbd4) scalars(N r2) stats(N fstat pv) nogaps star(* 0.10 ** 0.05 *** 0.01)
restore







********** ### Robustness check: vendor characteristics accounting for bed size or patient visits (Table A15-A16) ###
****** Bed size
reg newvmktshbed ivdombed ivmshbed if prechs==1&sysid==0

predict uhatbed,residual

gen uhatbed2=uhatbed*uhatbed
gen uhatbed3=uhatbed*uhatbed*uhatbed


preserve
rename (vmshbd1 vmshbd2 vmshbd3 vmshbd4) (vmshbd1_ori vmshbd2_ori vmshbd3_ori vmshbd4_ori)
rename (vmshbdbed1 vmshbdbed2 vmshbdbed3 vmshbdbed4) (vmshbd1 vmshbd2 vmshbd3 vmshbd4)


xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(newid) vce(r)  
est sto nonivbed

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0&uhatbed~=.,group(newid) vce(r)  
est sto nonivbed_iv

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* uhatbed uhatbed2 i.vid below50cat below75cat below100cat mkthhi* mktpop65* if prechs==1&sysid==0,group(newid) vce(boot,rep(100))  
est sto ivbed
restore



****** Patient visits
reg newvmktshpat ivdompat ivmshpat if prechs==1&sysid==0

predict uhatpat,residual

gen uhatpat2=uhatpat*uhatpat
gen uhatpat3=uhatpat*uhatpat*uhatpat


preserve

rename (vmshbd1 vmshbd2 vmshbd3 vmshbd4) (vmshbd1_ori vmshbd2_ori vmshbd3_ori vmshbd4_ori)
rename (vmshbdpat1 vmshbdpat2 vmshbdpat3 vmshbdpat4) (vmshbd1 vmshbd2 vmshbd3 vmshbd4)


xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0,group(newid) vce(r)  
est sto nonivpat

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* i.vid below50cat below75cat below100cat i.vid*hhi i.vid*totpop65 if prechs==1&sysid==0&uhatpat~=.,group(newid) vce(r)  
est sto nonivpat_iv


xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 bed* uhatpat uhatpat2 i.vid below50cat below75cat below100cat mkthhi* mktpop65* if prechs==1&sysid==0,group(newid) vce(boot,rep(100))  
est sto ivpat

restore



****** Correlation table (Table A16)
*** Overall correlation
corr newvmktsh newvmktshbed newvmktshpat if sysid==0
estout r(C,fmt(%9.3f)) using "./results/corr_mktsh.tex",replace style(tex)

*** Correlation by bed size quantile
mat A=J(5,2,.)
corr newvmktsh newvmktshbed if sysid==0
mat A[5,1]=r(rho)
corr newvmktsh newvmktshpat if sysid==0
mat A[5,2]=r(rho)

foreach i of num 1/4 {
corr newvmktsh newvmktshbed if bdrng==`i'&sysid==0
mat A[`i',1]=r(rho)
corr newvmktsh newvmktshpat if bdrng==`i'&sysid==0
mat A[`i',2]=r(rho)
}

estout matrix(A,fmt(%9.3f)) using "./results/corr_mktsh_quantile.tex",replace style(tex)





********** Policy functions for the alternative model specification: decreasing sunk costs (prepare for Table A12)
//Standalone, NA
cd ""  //target director

gen yrbar=2009
gen yrsince=(yrbar-year)*(year<=yrbar)
gen vdom_yrsince=newv1mkt*yrsince


forvalues i=6/9 {
preserve
set more off
keep if sysid==0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 vdom_yrsince i.vid*yrsince i.vid*bdtot i.vid*nprofit i.vid*teaching below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_na`i'_decsunk.csv",delim(",") novar replace 

restore
esttab ab using "./results/coef_sana_decsunk.csv", replace not nogaps compress nostar
}


//Standalone, AD
forvalues i=6/9 {
preserve
set more off
keep if sysid==0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat


xi:clogit newchoice vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd* vdom_yrsince i.vid*yrsince i.vid*bdtot i.vid*profit i.vid*teaching i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_a`i'_decsunk.csv",delim(",") novar replace

restore
esttab ab using "./results/coef_saad_decsunk.csv", replace not nogaps compress nostar
}


//Affiliated, NA
forvalues i=6/9 {
preserve
set more off
keep if sysid>0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit newchoice newvsysh i.vid*yrsince i.vid*bdtot i.vid*nprofit i.vid*perc_mcr below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_na`i'_decsunk.csv",delim(",") novar replace

restore
esttab ab using "./results/coef_afna_decsunk.csv", replace not nogaps compress nostar
}


//Affiliated, AD
forvalues i=6/9 {
preserve
set more off
keep if sysid>0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

xi:clogit newchoice newvsysh vlag i.vid*yrsince i.vid*bdtot i.vid*perc_mcr i.vid*perc_mcd i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
predict pr
keep if year==2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_a`i'_decsunk.csv",delim(",") novar replace
restore
esttab ab using "./results/coef_afad_decsunk.csv", replace not nogaps compress nostar
}






********** Policy functions for the alternative model specification: variable cost savings from the dominant local vendor (prepare for Table A13)
//Standalone, NA
cd ""  //target directory
gen v1mktbd=newv1mkt*bdtot


preserve
set more off
keep if sysid==0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1


xi:clogit choice vmshbd1 vmshbd2 vmshbd3 vmshbd4 v1mktbd i.vid*bdtot i.vid*profit i.vid*teaching i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_sana.csv", replace not nogaps compress nostar
predict pr
sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_na`i'.csv" if year==`yr',delim(",") novar replace 
}
restore



//Standalone, AD
*forvalues i=1999/2009 {
preserve
set more off
keep if sysid==0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1


xi:clogit choice vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd* v1mktbd i.vid*bdtot i.vid*profit i.vid*teaching i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_saad.csv", replace not nogaps compress nostar
predict pr
sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_a`i'.csv" if year==`yr',delim(",") novar replace 
}
restore



//Affiliated, NA
preserve
set more off
keep if sysid>0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit choice newvsysh below50cat below75cat below100cat i.vid*bdtot i.vid*nprofit i.vid*perc_mcr i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_afna.csv", replace not nogaps compress nostar
predict pr
sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_na`i'.csv" if year==`yr',delim(",") novar replace 
}
restore



//Affiliated, AD
preserve
set more off
keep if sysid>0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit choice newvsysh newvlag i.vid*bdtot i.vid*perc_mcr i.vid*perc_mcd i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_afad.csv", replace not nogaps compress nostar
predict pr
sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_a`i'.csv" if year==`yr',delim(",") novar replace 
}
restore





**********
**********
********** Prepare inputs for the IV sample
****** Generate IV-sample indicators (at the HSA level)
cd ""   //target direcotry
***!!! WANT indicates the IV SAMPLE 
gen want=(ivmsh~=.)

***!!! Also include affiliated hospitals that belong to the system included in the original minoraff!!
bys sysid year vid:egen withwant=max(want==1)
replace withwant=0 if sysid==0
***!!! KEEP the IV SAMPLE
keep if want==1|withwant==1


****** Market characteristics ### Table A18 ###
preserve
bys year:count if sysid==0
duplicates drop hsa year,force
bys year:sum nhosp nsa perc_adp perc_usedom totpop65_ori hhi
bys year:sum perc_usedom if totadp>0
restore


****** outfile mktvar_pophhi
preserve
keep if newchoice==1
sort ahaid year

gen mktcat=0
replace  mktcat=0 if below25cat==1
replace  mktcat=1 if below50cat==1
replace  mktcat=2 if below75cat==1
replace  mktcat=3 if below100cat==1


outfile ahaid year hhi mktcat totpop65 using "./prepare/mktvar_pophhi" if sysid==0,c replace

restore




****** outfile regvarYEAR_sing
forvalues i=6/9 {
preserve
keep if year==2000+`i'
keep if newchoice==1
keep if sysid==0
sort ahaid
outfile ahaid year bdtot prechs adj_vnd hsa using "./prepare/regvar`i'_sing",c replace
restore
 }


 


****** Policy functions 
//Standalone, NA
preserve
set more off
keep if sysid==0
keep if prechs==1


gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1

xi:clogit choice vmshbd1 vmshbd2 vmshbd3 vmshbd4 i.vid*bdtot i.vid*nprofit i.vid*teaching below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_sana.csv", replace not nogaps compress nostar
predict pr

sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_na`i'.csv" if year==`yr',delim(",") novar replace 
}
restore



//Standalone, AD
preserve
set more off
keep if sysid==0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat


xi:clogit choice vmshbd1 vmshbd2 vmshbd3 vmshbd4 vlagbd1 vlagbd2 vlagbd3 vlagbd4 i.vid*bdtot i.vid*profit i.vid*teaching i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_saad.csv", replace not nogaps compress nostar
predict pr

sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/prdprob_a`i'.csv" if year==`yr',delim(",") novar replace
}
restore




//Affiliated, NA 
preserve
set more off
keep if sysid>0
keep if prechs==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

replace below25cat=0 if vid==1
replace below50cat=0 if vid==1
replace below75cat=0 if vid==1
replace below100cat=0 if vid==1


xi:clogit choice newvsysh i.vid*bdtot i.vid*nprofit i.vid*perc_mcr below50cat below75cat below100cat i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_afna.csv", replace not nogaps compress nostar
predict pr

sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_na`i'.csv" if year==`yr',delim(",") novar replace
}
restore



//Affiliated, AD
preserve
set more off
keep if sysid>0
keep if prechs>1
drop if vid==1

gen mktcat1=below25cat
gen mktcat2=below50cat
gen mktcat3=below75cat
gen mktcat4=below100cat

xi:clogit choice newvsysh vlag i.vid*bdtot i.vid*perc_mcr i.vid*perc_mcd i.vid*hhi i.vid*totpop65,group(newid) vce(r)
est sto ab
esttab ab using "./results/coef_afad.csv", replace not nogaps compress nostar
predict pr

sort ahaid year vid
forvalues i=6/9 {
local yr=2000+`i'
sort ahaid vid
export delim ahaid hsa bdtot prechs pr nprofit profit teach hhi totpop65 perc_mcr perc_mcd mktcat2 mktcat3 mktcat4 sysid using "./prepare/syschs_a`i'.csv" if year==`yr',delim(",") novar replace
}
restore







